package com.codedancer.campus.common.config;

import com.codedancer.campus.common.config.RedisConfig;
import java.time.Duration;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class RedisConfiguration {

    @Autowired
    private RedisConfig redisConfig;

    @Bean
    public GenericObjectPoolConfig getPoolConfig() {
        // 配置redis连接池
        GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
        poolConfig.setMaxTotal(redisConfig.getRedisPoolConfig().getMaxActive());
        poolConfig.setMaxIdle(redisConfig.getRedisPoolConfig().getMaxIdle());
        poolConfig.setMinIdle(redisConfig.getRedisPoolConfig().getMinIdle());
        poolConfig.setMaxWaitMillis(redisConfig.getRedisPoolConfig().getMaxWait());
        return poolConfig;
    }

    private StringRedisTemplate getStringRedisTemplate(int database) {
        // 构建工厂对象
        RedisStandaloneConfiguration configuration = new RedisStandaloneConfiguration();
        configuration.setHostName(redisConfig.getHost());
        configuration.setPort(redisConfig.getPort());
        configuration.setPassword(RedisPassword.of(redisConfig.getPassword()));
        LettucePoolingClientConfiguration clientConfiguration = LettucePoolingClientConfiguration
            .builder()
            .commandTimeout(Duration.ofSeconds(redisConfig.getTimeout()))
            .poolConfig(getPoolConfig()).build();
        LettuceConnectionFactory factory = new LettuceConnectionFactory(configuration,
            clientConfiguration);
        // 设置使用的redis数据库
        factory.setDatabase(database);
        // 重新初始化工厂
        factory.afterPropertiesSet();
        return new StringRedisTemplate(factory);
    }

    /**
     * 验证码redisTemplate 0 database
     *
     * @return
     */
    @Bean(name = "smsRedisTemplate")
    public StringRedisTemplate getSmsRedisTemplate() {
        // 构建工厂对象
        return getStringRedisTemplate(redisConfig.getSmsDatabase());
    }

    /**
     * 浏览记录redisTemplate 1 database
     *
     * @return
     */
    @Bean(name = "viewRedisTemplate")
    public StringRedisTemplate getViewRedisTemplate() {
        // 构建工厂对象
        return getStringRedisTemplate(redisConfig.getViewDatabase());
    }

    /**
     * 评论记录redisTemplate 2 database
     *
     * @return
     */
    @Bean(name = "commentRedisTemplate")
    public StringRedisTemplate getCommentRedisTemplate() {
        // 构建工厂对象
        return getStringRedisTemplate(redisConfig.getCommentDatabase());
    }

    /**
     * 点赞记录redisTemplate 3 database
     *
     * @return
     */
    @Bean(name = "kudosRedisTemplate")
    public StringRedisTemplate getKudosRedisTemplate() {
        // 构建工厂对象
        return getStringRedisTemplate(redisConfig.getKudosDatabase());
    }

    /**
     * 数据缓存redisTemplate 4 database
     *
     * @return
     */
    @Bean(name = "dataCacheRedisTemplate")
    public StringRedisTemplate getDataCacheRedisTemplate() {
        // 构建工厂对象
        return getStringRedisTemplate(redisConfig.getDataCacheDataBase());
    }

}
